// The Vue build version to load with the `import` command
// (runtime-only or standalone) has been set in webpack.base.conf with an alias.
import Vue from 'vue'
import App from './App'
import axios from 'axios'
import store from './store/store'
import qs from 'querystring'
import baseJs from './components/base'
import router from './router'

Vue.use(baseJs)

Vue.config.productionTip = false

Vue.prototype.$http = axios.create({
  baseURL: 'http://laravel.test.com',
  timeout: 10000,
  responseType: 'text',
  headers: {
    'Content-Type': 'application/x-www-form-urlencoded'
  },
  validateStatus: function (status) {
    return true
  }
})

// 配置发送请求的信息
Vue.prototype.$http.interceptors.request.use(function (config) {
  config.data = qs.stringify(config.data)
  if (!config.url.indexOf('site/login')) {
    console.log(true)
  } else {
    var token = JSON.parse(localStorage.getItem('token'))
    config.params = {access_token: token}
  }
  return config
}, function (error) {
  return Promise.reject(error)
})

Vue.prototype.$http.interceptors.response.use(function (response) { // 配置请求回来的信息
                                                                    // stores.dispatch('loadingHide')
                                                                    // 返回的状态是401并且地址不是site/login重置登录状态并跳转到登录页面
  if ((response.status === 401) && (response.config.url.indexOf('site/login') === '-1')) {
    localStorage.removeItem('token')
    router.push('/login')
  }
  return response
}, function (error) {
  return Promise.reject(error)
})

/* eslint-disable no-new */
new Vue({
  el: '#app',
  router,
  store,
  template: '<App/>',
  components: { App }
})
